package test.baojia.act.business;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.WebDriver;

import java.util.Properties;

/**
 * 嵌套批量执行脚本测试，重构context。
 */
public class NestedBatchTestJsonBusiness extends SimpleBatchTestJsonBusiness {

    private final static String SUB_PARAM_NAME = "subParamName";

    @Override
    public void run(JSONObject context, WebDriver driver, Properties resource, JSONObject parentContext)throws  Exception  {
        JSONObject inheritContext = this.makeInheritContext(context , parentContext);
        super.run(inheritContext, driver, resource, parentContext);
    }

    protected JSONObject makeInheritContext(JSONObject context , JSONObject parentContext){
        JSONObject inheritContext = new JSONObject();

        if(parentContext != null){
            String subParamName = null;
            if(parentContext.containsKey(SUB_PARAM_NAME)){
                subParamName = parentContext.getString(SUB_PARAM_NAME);
            }
            else{
                subParamName = context.getString("business") + "Params";
                subParamName = subParamName.substring(0 , 1).toLowerCase() + subParamName.substring(1);
            }

            JSONObject newContext = parentContext.getJSONObject(subParamName);

            inheritContext.putAll(newContext);
        }

        inheritContext.putAll(context);

        return inheritContext;
    }
}